import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:common_utils/common_utils.dart';
import 'package:xinxuan/utils/color_util.dart';

class NetworkImageUtil extends StatelessWidget {
  
  /// 图片
  final String url;
  /// 用户名
  final String nickname;
  /// 图片宽度
  final double width;
  /// 图片高度
  final double height;
  /// 图片弧度
  final double borderRadius;
  /// 图片弧度
  final double topLeftRadius;
  /// 图片弧度
  final double topRightRadius;
  /// 图片弧度
  final double bottomLeftRadius;
  /// 图片弧度
  final double bottomRightRadius;
  /// 图片缩放方式
  final BoxFit fit;
  /// 是否带阴影
  final bool showShadow;

  NetworkImageUtil({
    required this.url, 
    required this.width, 
    required this.height, 
    this.borderRadius = 0, 
    this.topLeftRadius = 0, 
    this.topRightRadius = 0, 
    this.bottomLeftRadius = 0, 
    this.bottomRightRadius = 0, 
    this.nickname = '', 
    this.fit = BoxFit.cover,
    this.showShadow = true
  });

  @override
  Widget build(BuildContext context) {
    if (!TextUtil.isEmpty(nickname) && TextUtil.isEmpty(url)) {
      return Container(
        width: width,
        height: height,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(width),
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              myHeadColor1,
              myHeadColor2
            ]
          ),
          boxShadow: showShadow ? [
            BoxShadow(
              color: headShowShadow,
              offset: Offset(0, -2.h),
              blurRadius: 2.w
            )
          ] : []
        ),
        child: Text(
          '${nickname.substring(0, 1)}',
          style: TextStyle(
            color: textWhiteColor,
            fontSize: 22.sp,
            fontWeight: FontWeight.bold
          ),
        ),
      );
    }
    if (TextUtil.isEmpty(url)) {
      return ClipRRect(
        borderRadius: borderRadius != 0 ? BorderRadius.circular(borderRadius) :
        BorderRadius.only(
          topLeft: Radius.circular(topLeftRadius),
          topRight: Radius.circular(topRightRadius),
          bottomLeft: Radius.circular(bottomLeftRadius),
          bottomRight: Radius.circular(bottomRightRadius)
        ),
        child: Image.asset(
          'assets/icons/icon_damage.png',
          width: width,
          height: height,
          fit: fit,
        ),
      );
    }
    return ClipRRect(
      borderRadius: borderRadius != 0 ? BorderRadius.circular(borderRadius) :
      BorderRadius.only(
        topLeft: Radius.circular(topLeftRadius),
        topRight: Radius.circular(topRightRadius),
        bottomLeft: Radius.circular(bottomLeftRadius),
        bottomRight: Radius.circular(bottomRightRadius)
      ),
      child: CachedNetworkImage(
        imageUrl: url,
        width: width,
        height: height,
        fit: fit,
        errorWidget: (context, url, error) {
          return Image.asset(
            'assets/icons/icon_damage.png',
            width: width,
            height: height,
            fit: fit,
          );
        },
      )
    );
  }
}